//
//  JGProductCollectionViewController.m
//  WangYLottery
//
//  Created by JJetGu on 15/8/23.
//  Copyright (c) 2015年 JJetGu. All rights reserved.
//

#import "JGProductCollectionViewController.h"

#import "JGProduct.h"
#import "JGProductCell.h"

@interface JGProductCollectionViewController ()

@property (nonatomic, strong) NSMutableArray *products;

@end

@implementation JGProductCollectionViewController

- (NSMutableArray *)products
{
    if (_products == nil) {
        _products = [NSMutableArray array];
        
        
        NSString *fileName = [[NSBundle mainBundle] pathForResource:@"products.json" ofType:nil];
        NSData *data =  [NSData dataWithContentsOfFile:fileName];
        
        NSArray *jsonArr =  [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        
        for (NSDictionary *dict in jsonArr) {
            JGProduct *product = [JGProduct productWithDict:dict];
            [_products addObject:product];
        }
        
    }
    return _products;
}

static NSString * const reuseIdentifier = @"Cell";

/*
 使用UICollectionView
 第一步：必须有布局
 第二部：cell必须自己注册
 
 
 */

-(instancetype)init
{
    // 创建流水布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    // 每一个cell的尺寸
    layout.itemSize = CGSizeMake(80, 80);
    
    // 垂直间距
    layout.minimumLineSpacing = 10;
    
    // 水平间距
    layout.minimumInteritemSpacing = 0;
    
    // 内边距
    layout.sectionInset = UIEdgeInsetsMake(10, 0, 0, 0);
    
    return [super initWithCollectionViewLayout:layout];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Register cell classes
//    [self.collectionView registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    
    UINib *xib = [UINib nibWithNibName:@"JGProductCell" bundle:nil];
    [self.collectionView registerNib:xib forCellWithReuseIdentifier:reuseIdentifier];
    
     self.collectionView.backgroundColor = [UIColor whiteColor];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 1;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.products.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JGProductCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    // 获取模型
    JGProduct *p = self.products[indexPath.item];
    
    cell.product = p;
    
    return cell;
}

#pragma mark <UICollectionViewDelegate>
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    JGProduct *p = self.products[indexPath.item];
    NSLog(@"点击了---%@",p.title);
    
    NSString *urlStr = [NSString stringWithFormat:@"%@://%@",p.customUrl,p.ID];
    
    NSURL *url = [NSURL URLWithString:urlStr];
    
    UIApplication *app = [UIApplication sharedApplication];
    
    // 是否安装这个应用
    if ([app canOpenURL:url]) {
        // 安装了这个应用
        [[UIApplication sharedApplication] openURL:url];
    } else {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:p.url]];
    }
}

@end
